package io.renren.modules.substation.service.impl;

import io.renren.modules.substation.entity.TInfoAreaInfoEntity;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.substation.dao.TInfoDevInfoDao;
import io.renren.modules.substation.entity.TInfoDevInfoEntity;
import io.renren.modules.substation.service.TInfoDevInfoService;


@Service("tInfoDevInfoService")
public class TInfoDevInfoServiceImpl extends ServiceImpl<TInfoDevInfoDao, TInfoDevInfoEntity> implements TInfoDevInfoService {

    @Autowired
    private TInfoRoleAreaServiceImpl tInfoRoleAreaServiceImpl;

    private static boolean cacheDevInfoFlag = false;
    private static Map<String, TInfoDevInfoEntity> SysCacheDevMap;

    protected SysUserEntity getUser() {
        return (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
    }


    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        String devCode = (String)params.get("devCode");
        String devType = (String)params.get("devType");
        Long devArea = params.get("devArea") != null ? Long.parseLong(params.get("devArea").toString()) : null;
        List<Long> areaList = tInfoRoleAreaServiceImpl.getAreaListByUser(getUser().getUserId());

        IPage<TInfoDevInfoEntity> page = this.page(
                new Query<TInfoDevInfoEntity>().getPage(params),
                new QueryWrapper<TInfoDevInfoEntity>()
                        .eq(devCode!=null&&!devCode.isEmpty(),"dev_code",devCode)
                .eq(devType!=null&&!devType.isEmpty(),"dev_type",devType)
                .eq(devArea!=null,"dev_area",devArea)
                        .in("dev_area", areaList)
        );

        return new PageUtils(page);
    }

    @Override
    public List<TInfoAreaInfoEntity> queryNoAreaList() {
        return baseMapper.queryNoAreaList();
    }

    @Override
    public void updateAreaNullById(TInfoDevInfoEntity o) {
        baseMapper.updateAreaNullById(o);
    }

    @Override
    public void setAreaNullByAreaId(TInfoDevInfoEntity o) {
        baseMapper.updateAreaNullById(o);
    }

    @Override
    public TInfoDevInfoEntity queryDevInfoByCondition(TInfoDevInfoEntity tInfoDevInfoEntity)
    {
        return baseMapper.queryDevInfoByCondition(tInfoDevInfoEntity);
    }

    @Override
    public List<TInfoDevInfoEntity> queryDevListByCondition(TInfoDevInfoEntity tInfoDevInfoEntity)
    {
        return baseMapper.queryDevListByCondition(tInfoDevInfoEntity);
    }

    @Override
    public List<TInfoDevInfoEntity> getDevInfoByDevCode(String devCode) {
        List<TInfoDevInfoEntity> list = baseMapper.selectList(
                new QueryWrapper<TInfoDevInfoEntity>().eq("DEV_CODE", devCode)
        );
        return list;
    }

    @Override
    public List<TInfoDevInfoEntity> getDevListByArea(Long areaId) {
        List<TInfoDevInfoEntity> list = baseMapper.selectList(
                new QueryWrapper<TInfoDevInfoEntity>().eq(areaId!=null,"DEV_AREA", areaId)
        );
        return list;
    }

    @Override
    public TInfoDevInfoEntity getDevInfoByCode(String devCode) {
        if (!cacheDevInfoFlag) {
            getDevInfoAll();
        }
        Map<String, TInfoDevInfoEntity> map = SysCacheDevMap;
        return map.get(devCode);
    }

    private void getDevInfoAll() {
        Map<String, TInfoDevInfoEntity> map = new HashMap<>();
        List<TInfoDevInfoEntity> list = baseMapper.selectList(new QueryWrapper<>());
        for (TInfoDevInfoEntity obj : list) {
            map.put(obj.getDevCode(), obj);
        }
        SysCacheDevMap = map;
        cacheDevInfoFlag = true;
    }


}